package state

import (
	"errors"
	"fmt"
)

type tReadingState struct {
	context iFileStreamContext
	iBytesRead  int
}


func newReadingState(context iFileStreamContext) iFileStreamState {
	return &tReadingState{
		context,
		0,
	}
}

func (me *tReadingState) OpenRead() error {
	return errors.New(fmt.Sprintf("tReadingState.OpenRead, already reading %s", me.context.File()))
}

func (me *tReadingState) OpenWrite() error {
	return errors.New(fmt.Sprintf("tReadingState.OpenWrite, already reading %s", me.context.File()))
}

func (me *tReadingState) Read(p []byte) (n int, err error) {
	size := len(p)
	me.iBytesRead += size
	fmt.Printf("tReadingState.Read, file=%s, iBytesRead=%v\n", me.context.File(), me.iBytesRead)
	return size, nil
}

func (me *tReadingState) Write(p []byte) (n int, err error) {
	return 0, errors.New(fmt.Sprintf("tReadingState.Write, cannot write to %s", me.context.File()))
}

func (me *tReadingState) Close() error {
	fmt.Printf("tReadingState.Close, file=%s, iBytesRead=%v\n", me.context.File(), me.iBytesRead)
	me.context.Switch(newClosedState(me.context))
	return nil
}